package cpr.context.ruleacceptor;

import cpr.Printer;
import cpr.Rule;
import cpr.Utils;
import cpr.context.CacheRuleContext;
import cpr.context.Context;
import data.IWorksetItem;

public class WorksetItemRuleDigitalAcceptor implements IRuleAcceptor {
	IWorksetItem item;
//	int itemString;
	int ruleNr;
	int _ruleNrGlobal;
	Context ctx;
	public WorksetItemRuleDigitalAcceptor(Context ctx, IWorksetItem worksetItem) {
		item = worksetItem;
//		itemString = Utils.printRuleAccordingToVectorDigital(item.getClusterDescriptor().getRule(), item.getClusterDescriptor().getVector());
		ruleNr = 0;
		_ruleNrGlobal = 0;
		this.ctx =ctx;
		}
	int chcnt = 0;
	
	public int nextRuleGlobalNr() {
		int t = _ruleNrGlobal;
		_ruleNrGlobal ++;
		return t;
	}
	public boolean acceptGlobalNr(int globalNr) {
		return true;
	}
	
	public boolean accept(Rule aRule, int globalNr) {
		aRule.ruleNrGlobal = globalNr;
		
		if (!Utils.equals(item.getClusterDescriptor().getRule(), aRule, item.getClusterDescriptor().getVector())) {
			return false;
		}
		
		for (int i = 0; i < item.getChlids().size(); i++) {
			IWorksetItem child = (IWorksetItem)item.getChlids().get(i);
			
			int childString = Utils.printRuleAccordingToVectorDigital(
					child.getClusterDescriptor().getRule(), 
					child.getClusterDescriptor().getVector());
			
			int s2 = Utils.printRuleAccordingToVectorDigital(
					aRule, 
					child.getClusterDescriptor().getVector());
			
			
			String sx1 = ctx.getPrintContext().printRuleUsingDict(child.getClusterDescriptor().getRule(), child.getClusterDescriptor().getVector() );
			String sx2 = ctx.getPrintContext().printRuleUsingDict(aRule, child.getClusterDescriptor().getVector() );
			if (sx1.equals(sx2)) {
				chcnt ++;
			}
			
			int t =1;
			if (s2==childString) {
				return false;
			}
		}
		aRule.ruleNr = ruleNr;
		ruleNr ++;
		return true;
	}
	
}
